let express = require('express');
let router = express.Router();
let indexDao  =require('../../model/client/index')



//封装io
let returnAdminRouter = function(io) {
/**
 * @api {get} /api/client/images/list  获取所在单元的图片
 * @apiName 设备获取所在单元的图片
 * @apiGroup 门禁
 * @apiVersion 0.1.0 
 * @apiHeader   {String} Authorization    填写设备唯一编号
 * @apiSuccess {String} status 状态码
 * @apiSuccess {Json} msg  信息
 */

//设备获取所在单元的图片
router.get('/images/list' ,async function (req,res) {

   let imagesList  = await indexDao.imagesList();
   let  getImagesNewstime  = await indexDao.getImagesNewstime();
   let  latestTime ='';
        //设备同步后更改 图片同步状态
    if(imagesList.length>0){
    for (let index = 0; index < imagesList.length; index++) {
      let element = imagesList[index];
      await indexDao.updatedImagesStatus(element.id);
    }
    }
    if(getImagesNewstime.length>0){
      latestTime =getImagesNewstime[0].createdAt;
    }

  await res.json({status:200,msg:'success',latestTime:latestTime,data:imagesList})
})


/**
 * @api {get} /api/client/notice/list  获取所在小区的所有通知公告
 * @apiName 设备获取所在小区的所有通知公告
 * @apiGroup 门禁
 * @apiVersion 0.1.0 
 * @apiHeader    {String} Authorization   填写设备唯一编号
 * @apiSuccess {String} status 状态码
 * @apiSuccess {Json} msg  信息
 */

//获取该小区下的所有通知公告
router.get('/notice/list' ,async function(req,res){

    let noticeList  = await indexDao.noticeList();

    await res.json({status:200,msg:'success',data:noticeList});


})
/**
 * @api {get} /api/client/recover 设备图片信息核验接口
 * @apiName 设备图片信息核验接口
 * @apiGroup 门禁
 * @apiVersion 0.1.0 
 * @apiHeader    {String} Authorization   填写设备唯一编号
 * @apiSuccess {String} status 状态码
 * @apiSuccess {Json} msg  信息
 */
//设备信息recover

router.get('/recover', async function(req,res){
  let serialNum = req.headers.authorization;

  let validationEquipment = await indexDao.validationEquipment(serialNum);
   //获取此设备所对应单元的id
  let unitId =validationEquipment[0].unitId; 
    //获取设备最后一次掉线时间
  let lastDisconnectTime = validationEquipment[0].lastDisconnectTime;
  //获取所有图片信息
  let recoverData   =  await indexDao.getRecoverList(unitId,lastDisconnectTime);

  

  await res.json({status:200,msg:'success',data:recoverData});

})



return router;
};

module.exports = returnAdminRouter;